//  GameplayLayer.m
//  SpaceViking

#import "GameplayLayer.h"
//#import "LoSTree.h"


@implementation GameplayLayer


- (void)dealloc {
    [super dealloc];
}


#pragma mark –
#pragma mark Update Method
- (void) update:(ccTime)deltaTime {
    CCArray *listOfGameObjects = [sceneSpriteBatchNode children];                   
    for (GameCharacter *tempChar in listOfGameObjects) {        
        [tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];                      
    }
    Samurai *samurai = (Samurai*)[self getChildByTag:kSamuraiSpriteTagValue];
    [samurai updateStateWithDeltaTime:deltaTime];
    
//    CCLabelTTF *labelDeadTroop = (CCLabelTTF*)[self getChildByTag:102];
    [labelDeadTroop setString:[NSString stringWithFormat:@"%i / 20",samurai.deadTroop]];
}

#pragma mark -
- (void)createObjectOfType:(GameObjectType)objectType 
               withHealth:(int)initialHealth
               atLocation:(CGPoint)spawnLocation 
               withZValue:(int)ZValue {
    
    if (objectType == kEnemyTypeRadarDish) {

    } 
}

- (void)createPhaserWithDirection:(PhaserDirection)phaserDirection andPosition:(CGPoint)spawnPosition {
    CCLOG(@"Placeholder for chapter 5, see below");
    return;
}

-(id)init {
    self = [super init];
    if (self != nil) {
        CGSize screenSize = [CCDirector sharedDirector].winSize; 
        // enable touches
        self.isTouchEnabled = YES; 
        
        srandom(time(NULL)); // Seeds the random number generator
        
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {         
            [[CCSpriteFrameCache sharedSpriteFrameCache] 
             addSpriteFramesWithFile:@"mononofu.plist"];          
            sceneSpriteBatchNode = 
            [CCSpriteBatchNode batchNodeWithFile:@"mononofu.png"];
        } 
        else {
            // not support iphone
        }
        [self addChild:sceneSpriteBatchNode z:0];
        
        
        Samurai *samurai = [[Samurai alloc] 
                          initWithSpriteFrame:[[CCSpriteFrameCache 
                                                sharedSpriteFrameCache] 
                                               spriteFrameByName:@"samuraiCenter1.png"]]; 
        
        
        [samurai setPosition:ccp(screenSize.width/2, screenSize.height * 0.14f)]; 
        [samurai setCharacterHealth:100];
        
        [self addChild:samurai z:kSamuraiSpriteZValue tag:kSamuraiSpriteTagValue]; 
//        CCLOG(@"toa do samurai ben GamePlayLayer: %f - %f",samurai.position.x,samurai.position.y);
        
        //add radar
        CCSprite *radar = [[CCSprite alloc]initWithSpriteFrame:[[CCSpriteFrameCache 
                                                                sharedSpriteFrameCache] 
                                                               spriteFrameByName:@"radar.png"]]; 
        CCSprite *radarSlice = [[CCSprite alloc]initWithSpriteFrame:[[CCSpriteFrameCache
                                                                      sharedSpriteFrameCache]
                                                                     spriteFrameByName:@"radar_slice.png"]];
        radar.anchorPoint = ccp(0,0);
        [radar setPosition:ccp(screenSize.width - radar.contentSize.width, screenSize.height - radar.contentSize.height)];
        radarSlice.anchorPoint = ccp(1, 0);
        [radarSlice setPosition:ccp(radar.position.x + radar.contentSize.width/2, radar.position.y + radar.contentSize.height/2)];
        id action = [CCRotateBy actionWithDuration:2.5f angle:360];
        [radarSlice runAction:[CCRepeatForever actionWithAction:action]];
        [self addChild:radar z:100 tag:kRadarSpriteTagValue];
        [self addChild:radarSlice z:101];
        
        //add Text display how many Troop have been killed
        labelDeadTroop = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%i/ 20",samurai.deadTroop] 
                                         fontName:@"Arial" fontSize:60];
        // Use fixed ordinate 60,60 for label
        labelDeadTroop.position = ccp(100, 60);
        [self addChild:labelDeadTroop z:102];
        
        //add old tree
        TreeCache *treeCache = [[[TreeCache alloc] init]autorelease];
        [self addChild:treeCache z:150 tag:50];
        
        [self scheduleUpdate];
    }
    return self; 
}
@end
